﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetcodeTest._100._20
{
    public class Leetcode17
    {
        public void Test()
        {
            try
            {
                LetterCombinations("22");
            }
            catch (Exception ex)
            { }
        }
        /*
         17. Letter Combinations of a Phone Number

         Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

            电话号码拨号九宫格键盘
            Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
             */
        public IList<string> LetterCombinations(string digits)
        {
            if (string.IsNullOrEmpty(digits))
                return new List<string>();
            List<string> listString = new List<string>();
            foreach(char a in digits)
            {
                listString.Add(GetStringBydigit(a));
            }

            List<string> listRet = new List<string>();
            LoopNextChar("", listString, 0, listRet);
            return listRet;
        }

        private void LoopNextChar(string sourceStr, List<string> listLoopStr, int listIndex, List<string> listRet)
        {
            int index = listIndex;
            string tempStr;
            foreach(char tempChar in listLoopStr[listIndex])
            {
                if (listIndex == 0)
                    sourceStr = "";
                tempStr = sourceStr;
                tempStr += tempChar;
                if (listIndex < listLoopStr.Count - 1)
                {
                    LoopNextChar(tempStr, listLoopStr, listIndex+1, listRet);
                }
                else
                {
                    listRet.Add(tempStr);
                }
            }
        }

        private string GetStringBydigit(char digit)
        {
            switch(digit)
            {
                case '2':
                    return "abc";
                case '3':
                    return "def";
                case '4':
                    return "ghi";
                case '5':
                    return "jkl";
                case '6':
                    return "mno";
                case '7':
                    return "pqrs";
                case '8':
                    return "tuv";
                case '9':
                    return "wxyz";
            }
            return "";
        }
    }
}
